package com.shrct.businessprocess.purchase.impl;

import com.shrct.businessprocess.budget.RCCostBreakDown;
import com.shrct.businessprocess.common.CurrencyType;
import com.shrct.businessprocess.project.RCProject;
import com.shrct.businessprocess.project.RCProjectMoney;
import com.shrct.businessprocess.project.RCRealMoney;
import com.shrct.businessprocess.purchase.RCBaseProductPurchaseBudget;
import com.shrct.businessprocess.purchase.RCPay;
import com.shrct.businessprocess.purchase.RCTransferCostTransaction;
import com.shrct.businessprocess.purchase.RCTransforCostPayRequest;
import com.shrct.businessprocess.purchase.RCTransformCostPayProcess;
import com.shrct.businessprocess.purchase.impl.RCBaseProductPurchaseBudgetImpl;
import com.shrct.businessprocess.purchase.impl.RCTransforCostPayRequestImpl;
import com.shrct.businessprocess.purchase.proxy.RCTransferCostTransactionProxy;
import edu.fudan.mylang.pf.BaseModelObject;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.pf.IObjectFactory;
import javax.persistence.AssociationOverride;
import javax.persistence.AssociationOverrides;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.hibernate.annotations.DiscriminatorOptions;

@Entity
@DiscriminatorValue("RCTRANSFERCOSTTRANSACTION_TYPE")
@DiscriminatorOptions(force = true)
@DiscriminatorColumn(length = 64)
@Table(name = "rctransfercosttransaction")
public class RCTransferCostTransactionImpl extends BaseModelObject implements RCTransferCostTransaction {
  public RCTransferCostTransactionImpl() {
    super();
  }
  
  @JoinColumn(name = "sourceBudget_id")
  @ManyToOne(targetEntity = RCBaseProductPurchaseBudgetImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCBaseProductPurchaseBudget sourceBudget;
  
  public RCBaseProductPurchaseBudget getSourceBudget() {
    return this.sourceBudget;
  }
  
  public RCTransferCostTransaction setSourceBudget(final RCBaseProductPurchaseBudget sourceBudget) {
    this.sourceBudget = sourceBudget;
    return this;			
    
  }
  
  @JoinColumn(name = "targetBudget_id")
  @ManyToOne(targetEntity = RCBaseProductPurchaseBudgetImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCBaseProductPurchaseBudget targetBudget;
  
  public RCBaseProductPurchaseBudget getTargetBudget() {
    return this.targetBudget;
  }
  
  public RCTransferCostTransaction setTargetBudget(final RCBaseProductPurchaseBudget targetBudget) {
    this.targetBudget = targetBudget;
    return this;			
    
  }
  
  @Embedded
  @AttributeOverrides(value = { @AttributeOverride(name = "amount", column = @Column(name = "amount_amount") ), @AttributeOverride(name = "workaround", column = @Column(name = "amount_workaround") ) } )
  @AssociationOverrides(value = @AssociationOverride(name = "exchange", joinColumns = @JoinColumn(name = "amount_exchange") ) )
  private RCProjectMoney amount = com.shrct.businessprocess.project.RCProjectMoney.create();
  
  public RCProjectMoney getAmount() {
    return this.amount;
  }
  
  public RCTransferCostTransaction setAmount(final RCProjectMoney amount) {
    this.amount = amount;
    return this;			
    
  }
  
  private String comment;
  
  public String getComment() {
    return this.comment;
  }
  
  public RCTransferCostTransaction setComment(final String comment) {
    this.comment = comment;
    return this;			
    
  }
  
  public void commit() {RCTransforCostPayRequest rCTransforCostPayRequest=RCTransforCostPayRequestImpl.create(this.sourceBudget,getObjectFactory());
    final Procedure1<RCTransforCostPayRequest> _function = new Procedure1<RCTransforCostPayRequest>() {
        public void apply(final RCTransforCostPayRequest request) {
          RCBaseProductPurchaseBudget _budget = request.getBudget();
          request.setThebudget(_budget);
          RCProjectMoney _neg = RCTransferCostTransactionImpl.this.amount.neg();
          request.setAmount(_neg);
          request.setTransaction(RCTransferCostTransactionImpl.this);
          RCPay _createAndAddtoPurchasePays = request.createAndAddtoPurchasePays(RCTransferCostTransactionImpl.this.sourceBudget);
          final Procedure1<RCPay> _function = new Procedure1<RCPay>() {
              public void apply(final RCPay it) {
                RCProjectMoney _amount = request.getAmount();
                it.setProjectAmount(_amount);
                RCRealMoney _rCRealMoney = new RCRealMoney();
                final Procedure1<RCRealMoney> _function = new Procedure1<RCRealMoney>() {
                    public void apply(final RCRealMoney it) {
                      RCProjectMoney _amount = request.getAmount();
                      Double _hostAmount = _amount.toHostAmount();
                      it.setAmount(_hostAmount);
                      it.setExchangeRate(Double.valueOf(1.0));
                      RCCostBreakDown _cbd = RCTransferCostTransactionImpl.this.sourceBudget.getCbd();
                      RCProject _project = _cbd.getProject();
                      CurrencyType _currencyType = _project.getCurrencyType();
                      it.setCurrencyType(_currencyType);
                    }
                  };
                RCRealMoney _doubleArrow = ObjectExtensions.<RCRealMoney>operator_doubleArrow(_rCRealMoney, _function);
                it.setAmount(_doubleArrow);
              }
            };
          ObjectExtensions.<RCPay>operator_doubleArrow(_createAndAddtoPurchasePays, _function);
          request.setIsFinished(Boolean.valueOf(true));
        }
      };
    ObjectExtensions.<RCTransforCostPayRequest>operator_doubleArrow(rCTransforCostPayRequest, _function);RCTransforCostPayRequest rCTransforCostPayRequest_1=RCTransforCostPayRequestImpl.create(this.targetBudget,getObjectFactory());
    final Procedure1<RCTransforCostPayRequest> _function_1 = new Procedure1<RCTransforCostPayRequest>() {
        public void apply(final RCTransforCostPayRequest request) {
          RCBaseProductPurchaseBudget _budget = request.getBudget();
          request.setThebudget(_budget);
          request.setAmount(RCTransferCostTransactionImpl.this.amount);
          request.setTransaction(RCTransferCostTransactionImpl.this);
          RCPay _createAndAddtoPurchasePays = request.createAndAddtoPurchasePays(RCTransferCostTransactionImpl.this.sourceBudget);
          final Procedure1<RCPay> _function = new Procedure1<RCPay>() {
              public void apply(final RCPay it) {
                RCProjectMoney _amount = request.getAmount();
                it.setProjectAmount(_amount);
                RCRealMoney _rCRealMoney = new RCRealMoney();
                final Procedure1<RCRealMoney> _function = new Procedure1<RCRealMoney>() {
                    public void apply(final RCRealMoney it) {
                      RCProjectMoney _amount = request.getAmount();
                      Double _hostAmount = _amount.toHostAmount();
                      it.setAmount(_hostAmount);
                      it.setExchangeRate(Double.valueOf(1.0));
                      RCCostBreakDown _cbd = RCTransferCostTransactionImpl.this.sourceBudget.getCbd();
                      RCProject _project = _cbd.getProject();
                      CurrencyType _currencyType = _project.getCurrencyType();
                      it.setCurrencyType(_currencyType);
                    }
                  };
                RCRealMoney _doubleArrow = ObjectExtensions.<RCRealMoney>operator_doubleArrow(_rCRealMoney, _function);
                it.setAmount(_doubleArrow);
              }
            };
          ObjectExtensions.<RCPay>operator_doubleArrow(_createAndAddtoPurchasePays, _function);
          request.setIsFinished(Boolean.valueOf(true));
        }
      };
    ObjectExtensions.<RCTransforCostPayRequest>operator_doubleArrow(rCTransforCostPayRequest_1, _function_1);
  }
  
  public RCTransformCostPayProcess getProcess() {
    IGenericQuery<RCTransformCostPayProcess> query=getObjectFactory().createGenericQuery(RCTransformCostPayProcess.class,"select p from com.shrct.businessprocess.purchase.impl.RCTransformCostPayProcessImpl as p   where ((p.transaction) = (:this)) and  p.active = 1      ").setParameter("this",this);
    RCTransformCostPayProcess _uniqueResult = query.uniqueResult();
    return _uniqueResult;
  }
  
  public static RCTransferCostTransaction create(final IObjectFactory objectFactory) {
    com.shrct.businessprocess.purchase.RCTransferCostTransaction rCTransferCostTransaction = new com.shrct.businessprocess.purchase.impl.RCTransferCostTransactionImpl(
    );
    objectFactory.create(rCTransferCostTransaction);
    return rCTransferCostTransaction;			
    
  }
  
  public static RCTransferCostTransaction createRCTransferCostTransactionImpl(final IObjectFactory objectFactory, final Procedure1<RCTransferCostTransaction> init) {
    com.shrct.businessprocess.purchase.RCTransferCostTransaction rCTransferCostTransaction = new com.shrct.businessprocess.purchase.impl.RCTransferCostTransactionImpl(
    );
    objectFactory.create(rCTransferCostTransaction);
    init.apply(rCTransferCostTransaction);
    
    return rCTransferCostTransaction;			
    
  }
  
  public RCTransferCostTransactionProxy toProxy() {
    com.shrct.businessprocess.purchase.proxy.RCTransferCostTransactionProxy proxy = new com.shrct.businessprocess.purchase.proxy.RCTransferCostTransactionProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
